package ofpof.service;

import java.util.Date;

import ofpof.domain.CaptchaToken;
import ofpof.domain.dao.CaptchaDao;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CaptchaService {

	@Autowired
	private CaptchaDao captchaDao;

	public CaptchaToken findValidTokenById(ObjectId captchaId) {
		return captchaDao.findTokenById(captchaId);
	}

	public CaptchaToken createNewCaptcha() {
		CaptchaToken captchaToken = new CaptchaToken();
		captchaToken.setCaptchaText("new");//will be set by the captcha controller
		captchaToken.setCreatedOn(new Date());
		captchaToken.setId(new ObjectId());
		captchaDao.create(captchaToken);
		return captchaToken;
	}

	public void updateCaptcha(CaptchaToken captchaToken) {
		captchaDao.update(captchaToken);
	}
	

	public void removeCaptcha(String captchaIdAsStr) {
		ObjectId captchaId = new ObjectId(captchaIdAsStr);
		captchaDao.deleteById(captchaId);
		
	}

}
